package ru.dc;

import com.jgoodies.plaf.plastic.PlasticXPLookAndFeel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ru.dc.gui.swing.MainFrame;
import ru.dc.gui.swing.SearchWindow;
import ru.dc.object.FavouriteHub;
import ru.dc.object.Settings;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.Locale;

/**
 * Created by IntelliJ IDEA.
 * User: deady
 * Date: 31.10.2010
 * Time: 22:50:52
 */
public class Application {

    private static Application application;
    private ConnectionManager manager = new ConnectionManager();
    private ExternalListener externalListener;
    private DownloadQueue downloadQueue;
    private HubEventsListener hubEventsTranslator;
    private ExternalEventsListener externalEventsTranslator;
    private MainFrame mainFrame;
    private Settings settings;
    private Log logger = LogFactory.getLog(getClass());
    private File applicationHomeDir;
    private ExternalConnectionsListener externalConnectionsListener;
    private PublicHubsService publicHubsService;
    private FavouritesService favouritesService;


    public Application() {

        String userHome = System.getProperty("user.home");
        File userDir = new File(userHome);
        File appDir = new File(userDir, ".yadcc");
        if (appDir.exists()) {
            logger.info("Using "+appDir+" as application home dir");
            applicationHomeDir = appDir;
        }
        else {
            if (!appDir.mkdir()) {
                logger.warn("Can't create "+appDir+" for using as application home dir");
                applicationHomeDir = new File(System.getProperty("tmp"), "yadcc");
                applicationHomeDir.mkdirs();
                logger.info("Using "+appDir+" using as application home dir");
            }
            else {
                logger.info("Created "+appDir+" for using as application home dir");
                applicationHomeDir = appDir;
            }
        }


        settings = new Settings();
        settings.load();


        externalListener = new ExternalListener();
        externalListener.setPort(settings.getUdpPort());

        externalConnectionsListener = new ExternalConnectionsListener();

        hubEventsTranslator = new HubEventsListener();
        externalEventsTranslator = new ExternalEventsListener();
        CommandDispatchingThread.getInstance().addCommandListener(hubEventsTranslator);
        CommandDispatchingThread.getInstance().addCommandListener(externalEventsTranslator);

        downloadQueue = new DownloadQueue();


    }


    public void start() {
        try {
            UIManager.setLookAndFeel(new PlasticXPLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        publicHubsService = new PublicHubServiceImpl();
        ((PublicHubServiceImpl)publicHubsService).init();

        favouritesService = new FavouriteServiceImpl();
        ((FavouriteServiceImpl)favouritesService).init();

        mainFrame = new MainFrame();
        CommandDispatchingThread.getInstance().start();
        externalListener.start();
        externalConnectionsListener.start();
        mainFrame.setVisible(true);
        downloadQueue.start();


        for (FavouriteHub hub : favouritesService.getFavouriteHubs()) {
            if (hub.isAutoConnect()) {
                manager.addHubConnection(hub.getServerInfo());
            }
        }

    }

    public static Application getApplication() {
        return application;
    }

    public static void main(String[] args) {
        application = new Application();
        application.start();
    }

    public ConnectionManager getConnectionManager() {
        return manager;
    }

    public ExternalListener getExternalListener() {
        return externalListener;
    }

    public HubEventsListener getHubEventsTranslator() {
        return hubEventsTranslator;
    }

    public ExternalEventsListener getExternalEventsTranslator() {
        return externalEventsTranslator;
    }

    public MainFrame getMainFrame() {
        return mainFrame;
    }

    public void close() {
        externalListener.setStopped(true);
        CommandDispatchingThread.getInstance().requestStop();
        System.exit(0);
    }

    public DownloadQueue getDownloadQueue() {
        return downloadQueue;
    }

    public Settings getSettings() {
        return settings;
    }

    public File getApplicationHomeDir() {
        return applicationHomeDir;
    }

    public PublicHubsService getPublicHubsService() {
        return publicHubsService;
    }

    public FavouritesService getFavouritesService() {
        return favouritesService;
    }
}
